﻿using HCLSMod;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SY280DRIVER
{
    public partial class RunLogs : Form
    {
        private Mutex tThdMutex { get; } = new Mutex(false, "RunLogs20221117");
        private List<string> tMsgList { get; } = new List<string>();

        public RunLogs()
        {
            InitializeComponent();
        }

        public void putLogStr(string sLogInfo)
        {
            tThdMutex.WaitOne();
            try
            {
                tMsgList.Add(string.Format("[{0}]", DateTime.Now.ToString("HH:mm:ss fff")) + sLogInfo.Trim());
            }
            finally
            {
                tThdMutex.ReleaseMutex();
            }
        }

        private void buttonClearLog_Click(object sender, EventArgs e)
        {
            richTextBoxRunLogs.Clear();
        }

        private void tDeamon_Tick(object sender, EventArgs e)
        {
            if (tMsgList.Count>0)
            {
                tThdMutex.WaitOne();
                try
                {
                    richTextBoxRunLogs.AppendText(tMsgList[0] + "\r\n");
                    tMsgList.RemoveAt(0);
                    if (checkBoxAutoToBottom.Checked)
                        RichTextGoBottom.GoBottom(richTextBoxRunLogs);
                }
                finally
                {
                    tThdMutex.ReleaseMutex();
                }
            }
        }
    }
}
